 


using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using Core;
using IglooCoder.Commons.WcfNhibernate;
using Infrastucture;
using WcfService1.DTO;
using Omu.ValueInjecter;

namespace WcfService1
{
    // NOTE: You can use the "Rename" command on the "Refactor" Departemen to change the class name "DepartemenServices" in code, svc and config file together.
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    [NHibernateContext]
    public class DepartemenServices : IDepartemenServices
    {
        private DepartemenRepository DepartemenRepository;

        public DepartemenServices()
        {
            DepartemenRepository = new DepartemenRepository(new WcfSessionStorage());
        }

        
        public List<DepartemenDTO> GetList(int page, out long recordCount, string sortProperty = null, string sortType = "ASC")
        {
            var Departemens = new List<Departemen>();
            var Results = new List<DepartemenDTO>();

            if (string.IsNullOrEmpty(sortProperty))
            {
                Departemens = DepartemenRepository.GetDataPaged(page, 30).ToList();
            }
            else
            {
                Departemens = DepartemenRepository.GetDataPaged(page, 30, sortProperty, sortType).ToList();
            }

            recordCount = DepartemenRepository.RecordCount;

            Departemens.ForEach(x =>
            {

                DepartemenDTO DepartemenDto = new DepartemenDTO();
                DepartemenDto.InjectFrom(x);
                Results.Add(DepartemenDto);
            }
                );

            return Results;
        }

        public bool Save(DepartemenDTO DepartemenDTO)
        {
            bool res;
            try
            {
                var Departemen = DepartemenRepository.GetDataOne(x => x.Code == DepartemenDTO.Code);
              
                if (Departemen == null)
                {
                    Departemen = new Departemen();                    
                }
                Departemen.InjectFrom(DepartemenDTO);
                DepartemenRepository.SaveOrUpdate(Departemen);
                res = true;
            }
            catch (Exception)
            {

                res = false;
            }

            return res;
        }


        public bool Delete(DepartemenDTO DepartemenDto)
        {
            bool res;
            try
            {
                var Departemen = new Departemen();
                Departemen.InjectFrom(DepartemenDto);
                DepartemenRepository.Delete(Departemen);
                res = true;
            }
            catch (Exception)
            {
                res = false;
            }

            return res;
        }


        public List<DepartemenDTO> GetListByCriteria(Dictionary<String, Object> criteria, string expression, int page, out long recordCount)
        {
            List<Departemen> Departemens = new List<Departemen>();
            var Results = new List<DepartemenDTO>();
            Departemens = DepartemenRepository.GetDataPaged(criteria, expression, page, 30).ToList();
            recordCount = DepartemenRepository.RecordCount;
            Departemens.ForEach(x =>
                {

                    DepartemenDTO DepartemenDto = new DepartemenDTO();
                    DepartemenDto.InjectFrom(x);
                    Results.Add(DepartemenDto);
                }
            );

            return Results;
        }


        public List<DepartemenDTO> GetAllDepartemen()
        {
            var Departemens = DepartemenRepository.FetchAll().ToList();
            var Results = new List<DepartemenDTO>();
            Departemens.ForEach(x =>
                {

                    DepartemenDTO DepartemenDto = new DepartemenDTO();
                    DepartemenDto.InjectFrom(x);
                    Results.Add(DepartemenDto);
                }
            );

            return Results;
        }


        public DepartemenDTO GetDepartemen(String kodeDepartemen)
        {
            var Departemen = DepartemenRepository.GetDataOne(x => x.Code == kodeDepartemen);
            DepartemenDTO DepartemenDto = new DepartemenDTO();
            DepartemenDto.InjectFrom(Departemen);
            return DepartemenDto;
        }

        public List<DepartemenDTO> GetDepartemenList(string kodeDepartemen)
        {
            var Departemens = DepartemenRepository.GetDataList(x => x.Code.Contains(kodeDepartemen)).ToList();
            var Results = new List<DepartemenDTO>();
            Departemens.ForEach(x =>
                {

                    DepartemenDTO DepartemenDto = new DepartemenDTO();
                    DepartemenDto.InjectFrom(x);
                    Results.Add(DepartemenDto);
                }
            );

            return Results;
        }


		

    }

}


